Password Values

These fields both look like ordinary text fields, but the first is not. Type some text into the first field; only "dots" appear. Password values can be read from scripts, even if they can't be seen in the window.

Discussion

Sometimes you wish to have the user type a password or some other secure information into a form. You still need to be able to evaluate it (or have some server-side agent evaluate it), so you're not blocked from accessing the contents in a script. (This is not as insecure as it might seem. Users cannot determine what was typed in the password field by viewing the script in the browser.)

Netscape Navigator versions prior to 3.0 may not allow you to access the password information. You will need to check for these browsers and have your script adjust accordingly.

function entered(fieldName)
{
    var formNum = 0;
    if(fieldName.indexOf("2") != -1)
        formNum = 1;
    var val = eval("document.forms["+formNum+"]."+fieldName+".value");
    var len = val.length;
    alert("You typed \"" + val +
          "\" of length " + len)
    eval("document.forms["+formNum+"]."+fieldName+".focus()");
    eval("document.forms["+formNum+"]."+fieldName+".select()");
}

<FORM onSubmit='entered("PW"); return false'>
    <INPUT TYPE="PASSWORD" NAME="PW">
</FORM><P>

<FORM onSubmit='entered("PW2"); return false'>
    <INPUT TYPE="TEXT" NAME="PW">
</FORM>
Copyright ©2000 by Charles River Media, All Rights Reserved